package cn.hs.ph.servlet;


import cn.hs.ph.dao.impl.CommunityDaoImpl;
import cn.hs.ph.entity.Community;
import cn.hs.ph.util.Result;
import com.alibaba.druid.sql.visitor.functions.Now;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/community")
public class CommunityServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        Map<String,String> map = new HashMap<>();
        CommunityDaoImpl communityDao = new CommunityDaoImpl();
        String action = req.getParameter("action");
        if(action.equals("select")){
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String communityName = req.getParameter("communityName");
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("communityName",communityName);
            List<Community> list = communityDao.selectAll(map);
            int total = communityDao.selectCount(map);
            Map<Object,Object> map1 = new HashMap<>();
            map1.put("list",list);
            map1.put("total",total);
//            Result result = Result.success("查询成功");
//            map1.put("status",JSON.toJSONString(map1));
            Result result = Result.success(map1);
            resp.getWriter().write(JSON.toJSONString(result));
        }else if("update".equals(action)){
            String flag = req.getParameter("addOrUpdateFlag");
            String communityAddress = req.getParameter("communityAddress");
            String code = req.getParameter("communityAreaCode");
            int communityAreaCode=0;
            if(code!=null && !"".equals(code)){
                communityAreaCode=Integer.parseInt(code);
            }
            String communityCity = req.getParameter("communityCity");
            String communityCoordinates = req.getParameter("communityCoordinates");

            String communityDistrict = req.getParameter("communityDistrict");
            String id = req.getParameter("communityId");
            int communityId=0;
            if(id!=null && !"".equals(id)){
                communityId = Integer.parseInt(id);
            }
            String communityLatitude = req.getParameter("communityLatitude");
            String communityLongitude = req.getParameter("communityLongitude");
            String communityName = req.getParameter("communityName");
            String communityProvince = req.getParameter("communityProvince");
            String count = req.getParameter("communityShopCount");
            int communityShopCount = 0;
            if (count!=null && !"".equals(count)){
                communityShopCount = Integer.parseInt(count);
            }
            String status = req.getParameter("communityStatus");
            int communityStatus = 0;
            if (count!=null && !"".equals(status)){
                communityStatus = Integer.parseInt(status);
            }
            String userCount = req.getParameter("communityUserCount");
            int communityUserCount=0;
            if (count!=null && !"".equals(userCount)){
                communityUserCount = Integer.parseInt(userCount);
            }
            String distance = req.getParameter("distance");

            Community community = new Community(communityId,communityName,communityProvince,communityCity,communityAddress,communityAreaCode,communityCoordinates,communityDistrict,communityLatitude,communityLongitude,communityShopCount,communityStatus,communityUserCount,distance);
            if ("1".equals(flag)){
                int num = communityDao.insert(community);
                Result result = null;
                if(num!=0){
                    result = Result.success("添加成功");
                }else {
                    result = Result.error("添加失败");
                }
                resp.getWriter().write(JSON.toJSONString(result));

            }else if("2".equals(flag)){
                int num = communityDao.update(community);
                Result result = null;
                if(num!=0){
                    result = Result.success("修改成功");
                }else {
                    result = Result.error("修改失败");
                }
                resp.getWriter().write(JSON.toJSONString(result));
            }
        }else if ("delete".equals(action)){
            String id = req.getParameter("communityId");
            int communityId = 0;
            if (id!=null && !"".equals(id)) {
                communityId = Integer.parseInt(id);
            }
            int num = communityDao.delete(communityId);
            Result result = null;
            if(num!=0){
                result = Result.success("删除成功");
            }else {
                result = Result.error("删除失败");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        }
    }
}
